package com.yiren.manager.ctrl.test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderTestResultRecord;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.elder.YrElderTestRecordForm;
import com.yiren.manager.form.test.YrElderTestResultRecordForm;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.test.YrElderTestResultRecordService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("elderTestRecord")
@Controller
public class YrElderTestRecordController extends GenericController<YrElderTestRecordForm, YrElderTestRecordForm>{
	
	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrElderTestResultRecordService elderTestResultRecordService;
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrElderTestRecordForm> find(String searchVal) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrElderTestRecordForm> result = new HTTPResult<YrElderTestRecordForm>();
		String sqlWhere = "card_id=?1 OR name=?2";
		QueryResult<YrElder> qrElder = elderService.find(YrElder.class, sqlWhere, new Object[]{searchVal,searchVal});
		if(qrElder.getTotalRecord()==0){
			throwExp("查询不到相关老者信息");
		}
		YrElder elder = qrElder.getResultList().get(0);
		YrElderTestRecordForm form = new YrElderTestRecordForm();
		form.setElderName(elder.getName());
		form.setElderCard(elder.getCardId());
		form.setElderId(elder.getElderId());
		
		sqlWhere = "elderId=?1";
		LinkedList<String> groupBy = new LinkedList<String>();
		groupBy.add("testTimesNumber");
	
		QueryResult<YrElderTestResultRecord> qrElderTestResultRecord = elderTestResultRecordService.findByPage(YrElderTestResultRecord.class, sqlWhere, new Object[]{elder.getElderId()},groupBy,null,null);
		
		System.out.println("qrElderTestResultRecord:"+qrElderTestResultRecord.getResultList().size());
		form.setTimesNumber(qrElderTestResultRecord.getResultList().size());
		
		List<YrElderTestResultRecordForm> elderTestResultRecordForms = new ArrayList<YrElderTestResultRecordForm>();
		for(YrElderTestResultRecord elderTestResultRecord : qrElderTestResultRecord.getResultList()){
			YrElderTestResultRecordForm elderTestResultRecordForm = ObjectUtils.objectToObject(elderTestResultRecord, YrElderTestResultRecordForm.class);
			elderTestResultRecordForms.add(elderTestResultRecordForm);
		}
		
		form.setElderTestResultRecordForms(elderTestResultRecordForms);
		
		List<YrElderTestRecordForm> formList = new ArrayList<YrElderTestRecordForm>();
		formList.add(form);
		result.setData(formList);
		return result;
	}

	@Override
	public YrElderTestRecordForm toForm(YrElderTestRecordForm e) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YrElderTestRecordForm toEntity(YrElderTestRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
